package cn.itcast.usermanage.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.itcast.usermanage.mapper.AutoUserMapper;
import cn.itcast.usermanage.mapper.UserMapper;
import cn.itcast.usermanage.pojo.DatagridResult;
import cn.itcast.usermanage.pojo.User;
import cn.itcast.usermanage.service.UserService;
@Service("userService")
@Transactional
public class UserServiceImpl implements UserService{

	@Autowired
	private UserMapper userMapper;
	
	@Autowired
	private AutoUserMapper autoUserMapper;
	
	@Override
	public DatagridResult<User> userList(Integer page, Integer rows) {
		// TODO Auto-generated method stub
		
		PageHelper.startPage(page, rows);
		
		List<User> data = userMapper.queryUserList();
		
		PageInfo<User> users = new PageInfo<>(data);
		
		DatagridResult<User> result = new DatagridResult<User>(users.getTotal(),data);
		
		return result;
	}

	@Override
	public void insertUser(User user) {
		// TODO Auto-generated method stub
		user.setCreated(new Date());
		
		user.setUpdated(new Date());
		
		autoUserMapper.insertSelective(user);
	}

	@Override
	public void deteleUserById(String[] userIds) {
		// TODO Auto-generated method stub
		for (String id : userIds) {
			userMapper.deleteUserById(Long.parseLong(id));

		}
	}

	@Override
	public void deteleUserByIdSqlForeach(Long[] ids) {
		// TODO Auto-generated method stub
		for (Long long1 : ids) {
			
			autoUserMapper.deleteByPrimaryKey(long1);
		}
	}

	@Override
	public User queryByUserId(Long id) {
		User user = new User();
		user.setId(id);
		return autoUserMapper.selectOne(user);

	}

	@Override
	public void updateUser(User user) {
		user.setUpdated(new Date());
		autoUserMapper.updateByPrimaryKeySelective(user);
	}

}
